package org.luxor.commons.core.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * 打印启动完成日志
 *
 * @author Mr.Yan  @date: 2021/1/5
 */
@Component
@Order(value = -1)
public class ApplicationStartupRunner implements ApplicationRunner {
    private static final Logger log = LoggerFactory.getLogger(ApplicationStartupRunner.class);

    @Value("${server.port:8080}")
    private String port;

    @Value("${server.servlet.context-path:}")
    private String contextPath;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        if ("-1".equals(port)) {
            return;
        }
        String localUrl = String.format("http://%s:%s%s", localIp(), port, contextPath);
        log.info("Startup complete on path: [ {} ]", localUrl);
    }

    public static String localIp() {
        try {
            return InetAddress.getLocalHost().getHostAddress();
        } catch (UnknownHostException e) {
            return "127.0.0.1";
        }
    }

}
